import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import wab from './wab'
import gya from './gya'
import cuiyuan from './cuiyuan'
import store from '../store/index.js'
Vue.use(VueRouter)

//解决重复单机相同路由的报错
const originalPush = VueRouter. prototype.push
VueRouter.prototype.push = function push( location){
  return originalPush.call(this,location) .catch(err => err)
}

const routes = [
  {
    path: '/',
    name: 'Main',
    component: () => import('../views/Main'),
    children:[
      {
        path: '/',
        name: 'Home',
        component: Home,
        children:[
          {
            path:'/',
            component: ()=>import ('../views/Hometab1')
          },
          {
            path:'tab2',
            component: ()=>import ('../views/Hometab2')
          },
          {
            path:'tab3',
            component: ()=>import ('../views/Hometab3')
          },
        ]
      },
      {
        path:'classfy',
        component:()=>import('../views/Classfy')
      },
      {
        path: 'personal',
        component: () =>
            import ('../views/Personal.vue'),
      },
      {
        path: '/goshoppcar',
        component: () =>
            import ('../views/shoppcar.vue'),
      },
      {
        path:'checkout',
        component:()=>import('../views/Checkout')
      },{
        path:'checkout1',
        component:()=>import('../views/Checkout1')
      },
    ]
  },
  ...wab,
  ...gya,
  ...cuiyuan
]

const router = new VueRouter({
    // mode: "history", //切换histroy模式
    routes
})

//路由守卫
router.beforeEach((to,from,next)=>{ //路由的全局前置守卫
  // console.log(to,from);
  // path = '/checkout'   '/account'
  if(to.path.indexOf('/personal')==-1){ //判断用户访问的是不是购物车
      next()  //不是个人中心，直接放行
  }else{
      if(store.state.users.userInfo){ //判断登录状态
          next()
      }else{
          next('/login')
      }
  }
})


export default router